﻿Imports System.Data.SqlClient

Public Class frmListReligion

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.txtReligionEn.Text.Trim.Length = 0 Then
            MsgBox("Please fill in all fields")
        ElseIf Me.txtReligionAr.Text.Length = 0 Then
            MsgBox("Please fill in all fields")
        Else
            Try

                Me.Cursor = Cursors.WaitCursor
                Dim cmd As New SqlCommand("Insert Into Religion (ReligionEn,ReligionAr) Values (@ReligionEn,@ReligionAr) ", cnn)
                cmd.Parameters.AddWithValue("@ReligionEn", Me.txtReligionEn.Text.Trim)
                cmd.Parameters.AddWithValue("@ReligionAr", Me.txtReligionAr.Text.Trim)
                cnn.Open()
                cmd.ExecuteNonQuery()
                cnn.Close()

                FillReligion()

                Me.txtReligionEn.Clear()
                Me.txtReligionAr.Clear()
                Me.txtReligionEn.Focus()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Sub FillReligion()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Religion", cnn)
            Dim Reader As SqlDataReader

            Me.DataGridView1.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.DataGridView1.Rows.Add(New String() {Reader.Item(0), Reader.Item(1), Reader.Item(2)})
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.DataGridView1.SelectedRows.Count = 1 Then
            If MsgBox("Confirm delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    Me.Cursor = Cursors.WaitCursor
                    Dim cmd As New SqlCommand("Delete From Religion Where SNo=" & Me.DataGridView1.SelectedRows(0).Cells(0).Value, cnn)

                    cnn.Open()
                    cmd.ExecuteNonQuery()
                    cnn.Close()

                    FillReligion()

                    Me.Cursor = Cursors.Default
                Catch ex As Exception
                    Me.Cursor = Cursors.Default
                    If cnn.State = ConnectionState.Open Then
                        cnn.Close()
                    End If
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub

    Private Sub frmPosition_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillReligion()
    End Sub
End Class